package com.weidft.dal.craft.dao.ibatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.weidft.dal.craft.dao.IGrooveDAO;
import com.weidft.dal.craft.dataobject.GrooveDO;

public class IBatisGrooveDAO extends SqlMapClientDaoSupport implements IGrooveDAO {

    public Integer insertGrooveDO(GrooveDO grooveDO) {
        return (Integer) getSqlMapClientTemplate().insert("GROOVE.insert", grooveDO);
    }

    @SuppressWarnings("unchecked")
    public GrooveDO findGrooveDO(Integer craftCommonId, Integer position, Integer status) {
        Map<String, Integer> paramMap = new HashMap<String, Integer>(4);
        paramMap.put("craftCommonId", craftCommonId);
        paramMap.put("position", position);
        paramMap.put("status", status);

        List list = this.getSqlMapClientTemplate().queryForList("GROOVE.findByIdAndType", paramMap);
        if (list == null || list.size() == 0) {
            return null;
        }
        return (GrooveDO) list.get(0);
    }

    public int updateGrooveDO(GrooveDO grooveDO) {
    	Integer craftCommonId = grooveDO.getCraftCommonId();
    	Integer position = grooveDO.getPosition();
    	Integer status = grooveDO.getStatus();
    	if (craftCommonId!=null && position!=null && status!=null)
    	{
    		GrooveDO gdo = findGrooveDO(craftCommonId,position,status);
    		if (gdo==null)
    		{
    			return insertGrooveDO(grooveDO);
    		}
    		else
    		{
    			return this.getSqlMapClientTemplate().update("GROOVE.update", grooveDO);
    		}
    	}
    	else
    	{
    		return this.getSqlMapClientTemplate().update("GROOVE.update", grooveDO);
    	}
    }

    public int deleteGrooveDO(Integer craftCommonId) {
        return this.getSqlMapClientTemplate().delete("GROOVE.delete_groove", craftCommonId.intValue());
    }

}
